/**
 * @Description MyCustomerController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2018年5月17日 下午10:58:51
 */
package com.solution.wx.my.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.solution.admin.customer.entity.CustomerEntity;
import com.solution.admin.customer.service.CustomerService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 我的客户
 * 
 * @author xu_cc
 * @date 创建时间：2018年5月17日 下午10:59:18
 */
@RequestMapping("/wx/customer")
@Controller("myWxCustomerController")
public class MyCustomerController extends BaseController {

	@Autowired
	CustomerService customerService;

	/**
	 * 我的客户-我的下级客户
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年5月17日 下午11:10:53
	 */
	@RequestMapping("/myCustomer")
	public String toMyWxCustomerPage(HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String requestType = request.getParameter("requestType");

			String pageNumStr = request.getParameter("pageNum");
			if (StringUtil.isEmpty(pageNumStr)) {
				pageNumStr = "1";
			}
			int pageNum = Integer.parseInt(pageNumStr);
			int numPerPage = 10;
			PageParam pageParam = super.getPageParam(request);
			pageParam.setPageNum(pageNum);
			pageParam.setNumPerPage(numPerPage);

			String searchKey = request.getParameter("searchKey");
			String areaCode = request.getParameter("areaCode");
			String areaValue = request.getParameter("areaValue");
			String isValid = request.getParameter("isValid");
			// 当前用户的邀请码
			Long customerId = getCustomerId();
			com.solution.admin.customer.entity.CustomerEntity customer = customerService.getCustomerById(customerId);
			String invitationCode = customer.getInvitationCode();
			// 有效期内
			if (StringUtil.isNotNull(isValid)) {
				paramMap.put("isValid", "1");
			}
			paramMap.put("searchKey", searchKey);
			paramMap.put("areaCode", areaCode);
			paramMap.put("invitationCode", invitationCode);
			// paramMap.put("customerId", customerId);

			PageBean pageBean = customerService.listOfMyCustomerPage(pageParam, paramMap);
			for (Object entityObject : pageBean.getRecordList()) {
				CustomerEntity entity = (CustomerEntity)entityObject;
				entity.setUserName(entity.getUserName().substring(0,1)+"**");
				entity.setMobile(entity.getMobile().replaceAll("(\\d{3})\\d{6}(\\d{2})", "$1******$2"));
			}
			if ("sync".equals(requestType)) {
				// 如果是异步，直接写response
				try {
					response.getWriter().write(JSONObject.toJSON(pageBean).toString());
					response.getWriter().flush();
					response.getWriter().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				// 加载区域下拉列表
				List<Map<String, String>> areaList = customerService.listOfAreaCombo(paramMap);
				model.addAttribute("areaList", areaList);
				model.addAttribute("pageBean", pageBean);
				model.addAttribute("customerList", pageBean.getRecordList());
			}
			model.addAttribute("searchKey", searchKey);
			model.addAttribute("isValid", isValid);
			model.addAttribute("areaCode", areaCode);
			model.addAttribute("areaValue", areaValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "wx/my/customer/my_customer";
	}
}
